package com.next.aappublicapp.fragments;

import com.next.aap.client.NewsItem;
import com.next.aappublicapp.AapApplication;
import com.next.aappublicapp.PagerViewActivity;
import com.next.aappublicapp.R;
import com.next.aappublicapp.tasks.DownloadImageTask;
import com.next.aappublicapp.util.ImageUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class NewsFragment extends Fragment {
	/**
	 * The fragment argument representing the section number for this
	 * fragment.
	 */
	public static final String ARG_NEWS_ITEM = "ARG_NEWS_ITEM";

	private ImageView imageView;
	
	public NewsFragment() {
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.news_fragment, container, false);
		TextView dummyTextView = (TextView) rootView.findViewById(R.id.news_content);
		imageView = (ImageView)rootView.findViewById(R.id.itemImageBig);
		NewsItem newsItem = (NewsItem)getArguments().getSerializable(NewsFragment.ARG_NEWS_ITEM);
		dummyTextView.setText(newsItem.getContent());
		
		TextView sectionTitle = (TextView) rootView.findViewById(R.id.news_title);
		sectionTitle.setText(newsItem.getTitle());
		//new DownloadImageTask(imageView).execute(newsItem.getImageUrl());
		ImageLoader.getInstance().displayImage(newsItem.getImageUrl(), imageView);
		return rootView;
	}
	
    public Intent createShareIntent() {
        Intent shareIntent = createShareIntent(imageView,"Install AAP android app from http://bit.ly/fbappinstall and get realtime updates on news/video/facebook", "Install AAP android app from http://bit.ly/fbappinstall");
        return shareIntent;
    }
    
    protected Intent createShareIntent(ImageView imageView,String text,String title) {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
    	try{
    		ImageUtil.copyPrivateRawResourceToPubliclyAccessibleFile(this.getActivity(), imageView);
            Uri uri = Uri.fromFile(this.getActivity().getFileStreamPath(AapApplication.SHARED_FILE_NAME));
            shareIntent = createShareIntent(uri, text, title);
    	}catch(Exception ex){
    		ex.printStackTrace();
    	}
        return shareIntent;
    }
    protected Intent createShareIntent(Uri uri,String text,String title) {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
    	try{
            shareIntent.setType("*/*");
            shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
            shareIntent.putExtra(Intent.EXTRA_TEXT, text);
            shareIntent.putExtra(Intent.EXTRA_TITLE, title);
            shareIntent.putExtra(Intent.EXTRA_SUBJECT, title);
    	}catch(Exception ex){
    		ex.printStackTrace();
    	}
        return shareIntent;
    }

}
